package com.demo.strategy.holder;

import com.demo.strategy.adapter.OrderPayPostProcessAdapter;
import com.demo.strategy.annotation.OrderProcessor;
import com.demo.strategy.enums.Channel;
import com.demo.strategy.enums.OrderType;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderPayPostProcessorHolder {

    private static final Map<String, OrderPayPostProcessAdapter> HOLDER = new HashMap<>();

    public OrderPayPostProcessorHolder(List<OrderPayPostProcessAdapter> list) {
        if (list != null && list.size() > 0){
            for (OrderPayPostProcessAdapter orderPayPostProcessAdapter : list) {
                OrderProcessor orderProcessor = AnnotationUtils.findAnnotation(orderPayPostProcessAdapter.getClass(), OrderProcessor.class);
                if (orderProcessor != null){
                    OrderType[] orderTypeArr = orderProcessor.orderType();
                    Channel[] channelArr = orderProcessor.channel();
                    for (int i = 0; i < orderTypeArr.length; i++) {
                        for (int j = 0; j < channelArr.length; j++) {
                            //以orderType_channel作为key，如：1_wxgzh
                            String key = orderTypeArr[i].getType() + "_" + channelArr[j].getTag();
                            HOLDER.put(key, orderPayPostProcessAdapter);
                        }
                    }
                }
            }
        }
    }

    public OrderPayPostProcessAdapter getOrderPayPostProcessor(Integer orderType, String channelTag){
        String key = orderType + "_" + channelTag;
        return HOLDER.get(key);
    }
}
